package com.mycjj.android.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.bean.GetsPrizeBaseBean.GetsPrizeBean.PrizeBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

//元宝抽奖
@SuppressWarnings("all")
public class Score_GoldLottery extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_gold_lottery);
		initHeader();
		init();
		initData();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("元宝抽奖");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Score_GoldLottery.this.finish();
			}
		});
	}
	
	private GridView gridView;
	PrizeBean bean;
	private List<Integer> arrayIndex;
	private int currentCount = 0;
	private int maxCount = 0;
	private TextView tv_name;
	private TextView tv_price;
	private void init(){
		Intent intent = getIntent();
		if(intent != null){
			bean = (PrizeBean)intent.getSerializableExtra("data");
			maxCount = Integer.parseInt(bean.getTime_max());
		}
		currentCount = 0;
		tv_name = (TextView)findViewById(R.id.tv_name);
		tv_price = (TextView)findViewById(R.id.tv_price);
		tv_name.setText(bean.getName());
		tv_price.setText("价值"+bean.getPrice()+"元");
		arrayIndex = new ArrayList<Integer>();
		adapter = new LotteryAdapter(this);
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int pos,
					long arg3) {
				for(int i : arrayIndex){
					if(i == pos){
						Toast.makeText(Score_GoldLottery.this, "该卡牌已经翻过了!", Toast.LENGTH_SHORT).show();
						return;
					}else{
						continue;
					}
				}
				
//				currentCount ++;
//				if(currentCount == (maxCount + 1)){
//					Toast.makeText(Score_GoldLottery.this, "今天抽奖次数已用完!", Toast.LENGTH_SHORT).show();
//					currentCount --;
//					return;
//				}else{
					lottery(view , 0, pos);
//				}
				
				
			}
		});
	}
	
	private void initData(){
		
	}
	
	private void lottery(final View view,int is_new,final int pos){
		final View iv_default_lotter = view.findViewById(R.id.iv_default_lotter);
		final View iv_no_gift = view.findViewById(R.id.iv_no_gift);
		final View ll_gift_view = view.findViewById(R.id.ll_gift_view);
		final ImageView iv_pic = (ImageView)view.findViewById(R.id.iv_pic);
		imageLoader.displayImage(bean.getImage(), iv_pic);
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("id", bean.getId());
		params.put("is_new", String.valueOf(is_new));
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestGameLotteryWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>)dataManager.getBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						arrayIndex.add(pos);
						Object obj = baseBean.getData().get("level");
						if(obj != null){
							int level = Integer.parseInt(obj.toString());
							iv_default_lotter.setVisibility(View.GONE);
							if(level == -1){//未中奖
								iv_no_gift.setVisibility(View.VISIBLE);
								ll_gift_view.setVisibility(View.GONE);
							}else{
								iv_no_gift.setVisibility(View.GONE);
								ll_gift_view.setVisibility(View.VISIBLE);
							}
						}else{
							iv_no_gift.setVisibility(View.VISIBLE);
							ll_gift_view.setVisibility(View.GONE);
						}
					}else{
						Toast.makeText(Score_GoldLottery.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
				
			}
		});
	}
	
	private LotteryAdapter adapter;
	public class LotteryAdapter extends TAdapter<String>{

		public LotteryAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lottery_view, null);
				holder = new ViewHolder();
				
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			
			return convertView;
		}
		
		@Override
		public int getCount() {
			return 6;
		}
		
		
		class ViewHolder{
			ImageView pic;
			TextView tv_name;
			TextView tv_price;
			Button btn_exchage;
			
		}

	}
}
